home *** CD-ROM | disk | FTP | other *** search
/ Ham Radio 2000 #2 / Ham Radio 2000 - Volume 2.iso / HAMV2 / ANTENNA / YAGIU112 / YAGI.H < prev    next >
C/C++ Source or Header  |  1995-08-29  |  11KB  |  186 lines

  1. #include <stdio.h>
  2. #include "nrutil.h"
  3. #include "com_hack.h"
  4. #include "nr_hack.h"
  5.  
  6. #define TRUE 1
  7. #define FALSE 0
  8.  
  9. #define MAX_LINE  100  /* Maximum characters on one line of input file */
  10.  
  11. #define DRIVEN_LENGTH      1 /* What to move - see randomis.c */
  12. #define DRIVEN_POSITION    2
  13. #define REFLECTOR_LENGTH   4
  14. #define DIRECTOR_LENGTH    8
  15. #define DIRECTOR_POSITION 16
  16. #define ALL_ELEMENT_LENGTHS_IDENTICAL 32
  17. #define LINEAR_TAPER                  64
  18. #define RESONATE_DRIVEN              128
  19.  
  20. /* better criteria, see optimise and better.c */
  21.  
  22. #define GAIN                  1
  23. #define FB                    2
  24. #define RESISTANCE            4
  25. #define REACTANCE             8
  26. #define VSWR                 16
  27. #define SIDE_LOBE_LEVEL      32
  28. #define REASONABLE        32768 
  29.  
  30. #define REASONABLE_G
  31. #define REASONABLE_FB       27
  32. #define REASONABLE_R         5
  33. #define REASONABLE_X         5
  34. #define REASONABLE_SWR       1.1
  35. #define REASONABLE_SIDELOBE 23
  36.  
  37. #ifndef Z0
  38. #define Z0                             50
  39. #endif
  40. #define E_MAX 179
  41. #define H_MAX 60
  42.  
  43. #define EULER 0.57721566
  44.  
  45. #define MAX_DRIVEN                 6 /* There are 6 bits info on a driven ele */
  46. #define MAX_PARASITIC              4 /* But only 4 on a parasitic */
  47.  
  48. #define  X                              1/* index into driven and parasitic arrays */
  49. #define  Y                              2
  50. #define  LENGTH                     3
  51. #define    DIAMETER                     4
  52. #define    VOLTAGE_R                 5/* Volt and phase not on parasistic ele */
  53. #define    VOLTAGE_I                 6
  54. #define     REAL                          0
  55. #define     IMAGINARY                  1
  56. #define  HEADER_SIZE             100/* size of header in .out files in bytes */   
  57.  
  58. typedef struct element_data {double x,y,length;} fred;
  59.  
  60. typedef struct performance_data {double r,x,gain,fb,swr,sidelobe;} f4;
  61.  
  62. typedef struct flags {int aflg, bflg, cflg, dflg, eflg, errflg,fflg;
  63.         int gflg, hflg, iflg, jflg, kflg, lflg, mflg, nflg, oflg, pflg;
  64.         int qflg, rflg, sflg, tflg, uflg, vflg, wflg, xflg, yflg, zflg;
  65.         int Aflg, Bflg, Cflg, Dflg, Eflg, Fflg, Gflg, Hflg, Iflg, Jflg;
  66.         int Kflg, Lflg, Mflg, Nflg, Oflg, Pflg, Qflg, Rflg, Sflg, Tflg;
  67.         int Uflg, Vflg, Wflg, Xflg, Yflg, Zoflg;} f2;
  68.  
  69. typedef struct pattern {
  70.             double three_dB_E, three_dB_H;
  71.             double first_null_E, first_null_level_E;
  72.             double first_null_H, first_null_level_H;
  73.             double first_sidelobe_E, first_sidelobe_level_E;
  74.             double first_sidelobe_H, first_sidelobe_level_H;
  75.                                 }f3;
  76. int main(int argc, char **argv);
  77. int linear_current_optimisation_test(struct FCOMPLEX *cur, double *old_sd, int elements, int parasites, struct flags flag);
  78. void display_currents( struct FCOMPLEX *currents, int elements);
  79. void dynamic_changing_of_weights(int i, int div, struct performance_data *weight);
  80. double Cin(double x);
  81. double ci(double x);
  82. double new_length(double oldl,double old_dia, double lambda, double new_dia);
  83. void end_if_stop_exists(int *i, int iterations,int divisor);
  84. struct performance_data subtract_structures(struct performance_data a, struct performance_data b);
  85. void print_relavent_performance_data(FILE *fp, char *s, int i, struct flags flag, struct performance_data data,double fitness, int Z_Q, int fitness_Q);
  86. /* char *s, int i, structure flags flag, \
  87. structure performance_data data); */
  88. void set_mean_structure(struct FCOMPLEX input_impedance,double E_fwd, double E_back,struct flags flag, double pin,struct element_data *coordinates, struct FCOMPLEX *current, int elements, double f, double design_f, struct performance_data *mean);
  89. void test_for_stop_file(void);
  90. void set_performance_structures(struct performance_data *weight, struct performance_data *max, struct performance_data *best, struct performance_data *worst);
  91. void optimising_for(struct flags flag);
  92. double dB_down_from_peak(double x, double pin, struct  element_data *coordinates, struct FCOMPLEX *current,int elements, double f,double design_f);
  93. double find_max_sidelobe_fast(double gain, double pin,struct element_data *coordinates, struct FCOMPLEX *current, int elements, double frequency,double design_f);
  94. double find_max_sidelobe_slow(double gain, double pin,struct element_data *coordinates, struct FCOMPLEX *current, int elements, double frequency,double design_f);
  95. void copy_matrix(int length, int width, double **to, double **from);
  96. char *get_data_filenames(int argc, char **argv, char *input);
  97. char *string(long lower, long upper);
  98. void free_string(char *string, long lower, long upper);
  99. int get_number_of_elements(char *datafile, int *driven, int *parasitic);
  100. void read_yagi_data(char *line, char *file, double *frequency, double *min_frequency, double*max_frequency, double *step_frequency, int dr, double **dr_data, int parasitic, double **para_data, double *angular_step);
  101. void   self_impedance(int i, double frequency, int driven, int parasitic, double **data, double **impedance);
  102. void self2(double r, double l, double wavel, double *rin, double *xin);
  103. void   mutual_impedance(int i, int j, double frequency, int driven, int parasitic, double **d, double **p, double **impedance);
  104. void   fill_z_matrix(double frequency, int driven, int parasitic, double **d, double **p, double **impedance);
  105. void z21(double lamda, double d, double mean_length, double *r21, double *x21);
  106. double ci(double); 
  107. void fill_v_vector(int driven, int parasitic, double **d, double *v_vector);
  108. void write_header_to_disk(FILE *ofp, int elements, int driven,
  109. int parasitic, double min_frequency, double max_frequency, double frequency,
  110. double step_frequency, double angular_step);
  111. void write_vector(double *v, int elements, FILE *ofp);
  112. int  read_header(FILE *ifp, FILE *ofp, double *min_f, double *max_f, double *step_f, double *f, double *angular_step);
  113. void z_input(struct FCOMPLEX v, struct FCOMPLEX i, struct FCOMPLEX *z);
  114. void reflection_coefficient(struct FCOMPLEX z_input, double *mag, double *phase);
  115. double calculate_vswr(double magnitude);
  116. double calculate_power_input(double real_z, struct FCOMPLEX current);
  117. void   write_coordinates_of_elements_to_disk(FILE *ofp, int driven,
  118. int parasitic, double **driven_data, double **parasitic_data);
  119. void gain(double theta, double phi, double  pin, double normallised_f, struct
  120. element_data *cordinates,struct FCOMPLEX *current, int elements, double *E, double *H, double actual_frequency, double design_frequency);
  121. struct FCOMPLEX E_to_complex_power(struct FCOMPLEX x);
  122. struct FCOMPLEX **FCOMPLEXmatrix(long a, long b, long c, long d);
  123. void free_FCOMPLEXmatrix(struct FCOMPLEX **m, long nrl, long nrh, long ncl, long nch);
  124. void free_FCOMPLEXvector(struct FCOMPLEX *v, long nrl, long nrh);
  125. void free_element_data_vector( struct element_data *v, long nl, long nh);
  126. double change_max_percentage_changes(int i, int iterations, double orig_pc);
  127. struct FCOMPLEX *FCOMPLEXvector(long a, long b);
  128. struct element_data *element_data_vector(long low, long high);
  129. void print_z_matrix(double f, int elements, double **z_matrix);
  130. void write_gain_at_various_angles(FILE *gain_fp, double angular_step, double pin, double normalised_f, double f, struct element_data *coordinates, struct FCOMPLEX *current, int elements, double design_f);
  131.  
  132. void write_input_data_to_disk(FILE *fp, char *notes, double frequency, double min_f, double max_f, double step_f, int elements ,int driven,int parasitic, double angular_step,double **d, double **p, double scale_factor); 
  133. void randomise(int randomisation_method, double frequency, double percent, double **driven_data, double **parasitic_data, int driven, int parasitic);
  134. void automatic_enhancement(struct flags flag, double frequency, double **driven_data, double **parasitic_data, int driven, int parasitic, struct FCOMPLEX *voltage, struct FCOMPLEX *current, struct FCOMPLEX *input_Z, double *v, double **z, double **A, double *b, int *indx, struct element_data *coordinates);
  135.  
  136. /* int is_it_better(int criteria,struct performance_data new,struct performance_data best);  */
  137. int is_it_better(int criteria, struct performance_data n, struct performance_data b);
  138. double version(void);
  139. void get_command_line_options(int argc, char **argv, struct flags *f);
  140. void write_data_for_gnuplot(FILE *fp1,FILE  *fp2,FILE *fp3,FILE *fp4,FILE *fp5,FILE *fp6, double f,double normalised_f,double input_impedance_r, double input_impedance_i, double vswr, double gain_E_plane,double  fb_ratio);
  141. void usage_first(char *str);
  142. void usage_input(char *str);
  143. void usage_yagi(char *str);
  144. void usage_output(char *str);
  145. void usage_optimise(char *str);
  146. void write_file_for_gnuplot_to_load(FILE *fp, char *filename);
  147.  
  148. double performance(struct flags flag, struct performance_data data,struct performance_data weights, struct performance_data max, struct performance_data start);
  149. int getoptions(int argc, char **argv, char *opts);
  150.  
  151. void copy_complex_data_to_real_matrix(int elements, double **from, double **to);
  152. void show_all_first_parameters(char *exefile);
  153. void show_all_optimise_parameters(char *exefile,struct flags flag);
  154. double randreal(void);
  155. int    randint(void);
  156. void solve_equations(double frequency, int driven, int parasitic, double **driven_data, double **parasitic_data, double *v, double **z, double *pin, struct FCOMPLEX *voltage, struct FCOMPLEX *current, struct FCOMPLEX *input_impedance, struct element_data *coordinates, double **A, double *b, int *indx);
  157. void genetic_algorithm(char *output_filename, char *update_filename, struct flags flag, double frequency, double minf, double maxf, double stepf, double angular_step, int driven ,int  parasitic, double **driven_data, double **parasitic_data, double *v, double **zz, double
  158. *pin, struct FCOMPLEX *voltage, struct FCOMPLEX *current, struct FCOMPLEX
  159. *input_impedance, struct element_data *coordinates, double **A, double *b, int
  160. *indx,struct performance_data *mean_performance);
  161.  
  162. double get_genetic_algorithm_fitness(struct flags flag, double frequency, int driven, int parasitic, double **driven_data, double **parasitic_data, double *v, double **z, double *pin, struct FCOMPLEX *voltage, struct FCOMPLEX *current, struct FCOMPLEX *input_impedance, struct element_data *coordinates, double **A, double  *b, int *indx, struct performance_data *data2);
  163.  
  164. void check_flags(struct flags flag, int argc, int optind, char *exefilename);
  165. void error_message(char *str);
  166. double determine_maximum_gain(double f, double  l);
  167. double determine_maximum_gain2(int elements);
  168.  
  169.  
  170. void do_since_better(int i, char *output_filename, char *update_filename, struct FCOMPLEX input_impedance, struct performance_data n,struct flags flag,char * notes,double frequency, double min_frequency,double max_frequency,double  step_frequency,int elements, int driven,int parasitic,double angular_step,double **driven_data,double **parasitic_data,double scale_factor,double new_performance);
  171. double gaussian();
  172. void sensitivity(double boom_sd, double length_sd, double **driven_data, double**parasitic_data, int driven, int parasites);
  173. double log2(double x);
  174. void mprove(double **a ,double **LU_of_a,int n, int *indx, double *b, double *x);
  175. void seedRNG(void);
  176. double error_3dB_E(double x); 
  177. double error_3dB_H(double x); 
  178. double zbrent(double (*func)(), double x1, double x2, double tol);  
  179. double Objective(char *gene);
  180. int GA_Free(void);
  181. int GA_Error(char *error_mesg);
  182. void SetPrint(int a);
  183. int Initialise(int popsise, int genesize);
  184. int Selection(FILE *fd, int gene);
  185. double ss2r(char *string, int pos, int len);
  186.